﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace IceAge
{
    public partial class MDIForm : Form
    {
        private TreeMenu tree_menu = new TreeMenu();
        private ReportMenu report_menu = new ReportMenu();
        public MDIForm()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            
        }

        private void OpenFile(object sender, EventArgs e)
        {
            
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
                // TODO: Add code here to save the current contents of the form to a file.
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: Use System.Windows.Forms.Clipboard to insert the selected text or images into the clipboard
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: Use System.Windows.Forms.Clipboard to insert the selected text or images into the clipboard
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: Use System.Windows.Forms.Clipboard.GetText() or System.Windows.Forms.GetData to retrieve information from the clipboard.
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void MDIForm_Load(object sender, EventArgs e)
        {            
            tree_menu.Show(dockPanel1, DockState.DockLeftAutoHide);
            report_menu.Show(dockPanel1, DockState.DockRightAutoHide);
        }

        private void functionMenuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tree_menu.DockState == DockState.Hidden)
                //tree_menu.Show(dockPanel1, DockState.DockLeftAutoHide);
                tree_menu.DockState = tree_menu.VisibleState;

            tree_menu.Activate();
        }

        private void reportMenuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (report_menu.DockState == DockState.Hidden)
                //tree_menu.Show(dockPanel1, DockState.DockLeftAutoHide);
                report_menu.DockState = report_menu.VisibleState;

            report_menu.Activate(); 
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {           
            Login login = new Login();
            if (login.ShowDialog() == DialogResult.OK)
            {
                changePasswordToolStripMenuItem.Enabled = true;
                DataSet user_info = CommonLibrary.GetDataSetFromProcedure("p_sel_IsAdministrator",
                    "'" + login.UserId + "'");
                if (Convert.ToInt16(user_info.Tables[0].Rows[0]["IsAdministrator"]) == 1)
                {
                    beginOfDayToolStripMenuItem.Enabled = true;
                    beginOfDayToolStripMenuItem.Visible = true;                    
                    CommonLibrary.BRANCH_ID = user_info.Tables[0].Rows[0]["BranchId"].ToString();
                }
                CommonLibrary.USER_NAME = login.UserId;
                InitData();
            }
        }

        private void InitData()
        {
            
            //System Parameters
            DataSet v_data = CommonLibrary.GetDataSetFromProcedure("sp_sel_SystemParameters", string.Empty);
            if (v_data.Tables[0].Rows.Count > 0)
            {
                CommonLibrary.SYSTEM_STATUS = v_data.Tables[0].Rows[0]["status"].ToString();
                CommonLibrary.TRAN_DATE =Convert.ToDateTime( v_data.Tables[0].Rows[0]["trans_date"]);
                CommonLibrary.SETTLE_DATE = Convert.ToDateTime(v_data.Tables[0].Rows[0]["settle_date"]);
                CommonLibrary.FIRM_CODE = v_data.Tables[0].Rows[0]["firm_code"].ToString();
                CommonLibrary.COMPANY_NAME = v_data.Tables[0].Rows[0]["company_name"].ToString();
                CommonLibrary.ADDRESS = v_data.Tables[0].Rows[0]["address"].ToString();
                CommonLibrary.CONTACT = v_data.Tables[0].Rows[0]["contact"].ToString();
                CommonLibrary.TRADING_STATUS = v_data.Tables[0].Rows[0]["trading_status"].ToString();
            }

            if (CommonLibrary.SYSTEM_STATUS != "A")
            {
                MessageBox.Show("He thong chua duoc bat dau ngay hoac khong phai la ngay lam viec",
                   this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                CommonLibrary.STOCK_INFO =
                //CommonLibrary.ExecuteReader("0202", new object[2] { string.Empty, CommonLibrary.TRAN_DATE });
                CommonLibrary.GetDataSetFromProcedure("p_sel_Securities", "");
                CommonLibrary.XML_DATA = CommonLibrary.GetDataSetFromProcedure("p_sel_XMLGrid", string.Empty);
                CommonLibrary.XML_DATA.Tables[0].TableName = "GridSchema";
                tree_menu.Restart(CommonLibrary.USER_NAME);
                report_menu.Restart(CommonLibrary.USER_NAME);
                toolStripStatusLabelAccount.Text = CommonLibrary.USER_NAME;
                toolStripStatusLabelMarketDate.Text = CommonLibrary.TRAN_DATE.ToString("dd/MM/yyyy");
                toolStripStatusSettle.Text = CommonLibrary.SETTLE_DATE.ToString("dd/MM/yyyy");
                toolStripStatusBranchId.Text = CommonLibrary.BRANCH_ID;
                this.Text = CommonLibrary.COMPANY_NAME + " - " + CommonLibrary.ADDRESS + " - " + CommonLibrary.CONTACT;
            }      
        }
        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Restart(); 
        }

        //private void placeBuyOrderToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    foreach (DockContent childForm in dockPanel1.Contents)
        //    {
        //        if (childForm.Name == "PlaceOrderX")
        //        {
        //            if (((Trading.PlaceOrderX)childForm).Side == "B")
        //            {
        //                childForm.Activate();
        //                return;
        //            }
        //        }
        //    }
        //    Trading.PlaceOrderX obj = new IceAge.Trading.PlaceOrderX();
        //    obj.Text = "Đặt lệnh mua";
        //    obj.Show(dockPanel1, DockState.DockBottom);
        //}

        //private void placeSellOrderToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    foreach (DockContent childForm in dockPanel1.Contents)
        //    {
        //        if (childForm.Name == "PlaceOrderX")
        //        {
        //            if (((Trading.PlaceOrderX)childForm).Side == "S")
        //            {
        //                childForm.Activate();
        //                return;
        //            }
        //        }
        //    }
        //    Trading.PlaceOrderX obj = new IceAge.Trading.PlaceOrderX();
        //    //obj.MdiParent = this;
        //    obj.Side = "S";
        //    obj.Text = "Đặt lệnh bán";
        //    obj.Show(dockPanel1, DockState.DockBottom);
        //}

        private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangePassword obj = new ChangePassword();
            obj.ShowDialog();
        }

        private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Help.ShowHelp(this, "Vegeta.chm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButtonSearch_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F9}");  
        }

        private void searchToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F9}");  
        }

        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F10}");
        }

        private void toolStripButtonExport_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F10}");
        }

        private void toolStripButtonCommit_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F11}");
        }

        private void commitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F11}");
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F12}");
        }

        private void toolStripButtonImport_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F12}");
        }

        private void beginOfDayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Administration.BeginOfDay obj = new Administration.BeginOfDay();
            obj.ShowDialog(this);
            if (obj.DialogResult == DialogResult.OK)
                InitData();
        }

        private void toolStripButtonAddnew_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F8}");
        }
    }
}
